package com.zyj.classic;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierTest {
    public static void main(String[] args) throws InterruptedException {
        //功能凑够5个线程再一并执行
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        for (int i = 1; i <= 1000; i++) {
            TimeUnit.MILLISECONDS.sleep(200);//隔200ms启动一个线程
            int finalI = i;
            new Thread(() -> {
                try {
                    cyclicBarrier.await();//线程会在这阻塞, 直至凑齐5个线程, 他们会一起解除阻塞向下执行
                    System.out.println("go " + finalI);
                } catch (Exception e) {
                }
            }).start();
        }


        TimeUnit.SECONDS.sleep(100);
    }
}
